﻿package net.wangit.jdbc.util;

import java.sql.Time;
import java.sql.Timestamp;

import net.wangit.util.Checker;
import net.wangit.util.DateUtil;


public class SqlDateUtil {
	public static java.sql.Date getSqlDate(String dateStr) {
		if (Checker.isDate(dateStr)) {
			try {
				return new java.sql.Date(DateUtil.getDateFormat()
						.parse(dateStr).getTime());
			} catch (Exception e) {
				return null;
			}
		}
		return null;
	}

	public static Time getSqlTime(String dateStr) {
		if (Checker.isTime(dateStr)) {
			try {
				return new Time(DateUtil.getTimeFormat().parse(dateStr)
						.getTime());
			} catch (Exception e) {
				return null;
			}
		}
		return null;
	}

	public static Timestamp getSqlTimestamp(String dateStr) {
		if (Checker.isTimestamp(dateStr))
			try {
				if (dateStr.length() == 19)
					dateStr = dateStr + ".000";
				else if (dateStr.length() > 20) {
					while (dateStr.length() < 23) {
						dateStr = dateStr + "0";
					}
				}
				return new Timestamp(DateUtil.getTimestampFormat().parse(
						dateStr).getTime());
			} catch (Exception e) {
				return null;
			}
		if (Checker.isDate(dateStr)) {
			try {
				return new Timestamp(DateUtil.getDateFormat().parse(dateStr)
						.getTime());
			} catch (Exception e) {
				return null;
			}
		}
		return null;
	}
}